# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from autoscrapy import config

def _create_engine(url):
    global ENGIN
    if ENGIN is None:
       ENGIN = create_engine(url)

# global base mode
BASE = declarative_base()
ENGIN = None
SESSION = None

def get_session():
    global ENGIN
    global SESSION

    if SESSION:
        return SESSION

    _create_engine(config.db_url)
    session = sessionmaker()
    session.configure(bind=ENGIN)
    SESSION = session()
    return SESSION

